HistoryManager.for   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
import { BoostHistory, BoostHistoryItem } from '@/types/BoostHistory';
2
import { existsSync, readFileSync, writeFileSync } from 'fs';
3
4
export class HistoryManager {
5
    public data: BoostHistory = [];
6
7
    constructor(public filename: string) {
8
        this.load();
9
    }
10
11
    public for(boostName: string): BoostHistory {
12
        return this.data.filter(item => item.boost === boostName);
13
    }
14
15
    public createEntry(item: BoostHistoryItem): BoostHistoryItem {
16
        this.data.push(item);
17
18
        return new Proxy(this.data[this.data.length - 1], {} as any);
19
    }
20
21
    public save() {
22
        if (this.filename.length === 0) {
23
            return;
24
        }
25
26
        writeFileSync(this.filename, JSON.stringify(this.data), { encoding: 'utf8' });
27
    }
28
29
    public load() {
30
        if (this.filename.length === 0) {
31
            return;
32
        }
33
34
        if (!existsSync(this.filename)) {
35
            this.save();
36
        }
37
38
        this.data = JSON.parse(readFileSync(this.filename, { encoding: 'utf8' }));
39
    }
40
}
41